﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventFactory
{
    static ObjectPool<CustomEvent> customEventPool = new ObjectPool<CustomEvent>(null,(t)=>t.Clear());

    public static CustomEvent CreatCustomEvent()
    {
        if (customEventPool.countInactive>0)
        {
             return customEventPool.Get();
        }
        else
        {
            return new CustomEvent();
        }
        
    }
    public static void RealseCustomEvent(CustomEvent customEvent)
    {
        customEventPool.Release(customEvent);
    }
}
public class EventFactory<T>
{
    static ObjectPool<CustomEvent<T>> customEventPool = new ObjectPool<CustomEvent<T>>(null, (t) => t.Clear());

    public static CustomEvent<T> CreatCustomEvent()
    {
        if (customEventPool.countInactive > 0)
        {
            return customEventPool.Get();
        }
        else
        {
            return new CustomEvent<T>();
        }

    }
    public static void RealseCustomEvent(CustomEvent<T> customEvent)
    {
        customEventPool.Release(customEvent);
    }
}


public class CustomEvent<T>
{
    public List<Action<T>> actions=new List<Action<T>>();

    public void AddListener(Action<T> t)
    {
        actions.Add(t);
    }
    public void RemoveListener(Action<T> t)
    {
        actions.Remove(t);
    }
    public void Invoke(T t)
    {

        for (int i = 0, j = actions.Count; i < j; i++)
        {
            actions[i](t);
        }
    }
    public void Clear()
    {
        actions.Clear();
    }

}
public class CustomEvent
{
    private List<Action> actions=new List<Action>();

    public void AddListener(Action t)
    {
        actions.Add(t);
    }
    public void RemoveListener(Action t)
    {
        actions.Remove(t);
    }
    public void Clear()
    {
        actions.Clear();
    }
    public void Invoke()
    {

        for (int i = 0, j = actions.Count; i < j; i++)
        {
            actions[i]();
        }
    }

}